package com.anan.tankGame;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.physics.BoundingShape;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;


/**
 * @author zhouGuoAn
 * @since 2024/9/1 21:03
 **/
public class TankApp extends GameApplication {

    private Entity cxkEntity;//蔡徐坤坦克实体

    private boolean isMoving = false; //是否正在移动

    /**
     * 初始化页面设置
     */
    @Override
    protected void initSettings(GameSettings gameSettings) {
        gameSettings.setTitle("CXK Game");
        gameSettings.setWidth(800);
        gameSettings.setHeight(600);
        gameSettings.setVersion("0.1");
        //框架的规则，需要遵守否则不显示图片
        //assets是所有资源的父目录
        //textures是用于存储图片的目录
        gameSettings.setAppIcon("img.png");//图标
    }

    /**
     * 初始化游戏资源信息
     */
    @Override
    protected void initGame() {
        //初始化蔡徐坤图片
        cxkEntity = FXGL.entityBuilder()
                //view 决定的是游戏实体的外观，无大小
                .view("img.png")
                //bbox决定游戏实体的真实大小
                .bbox(BoundingShape.box(132, 143))
                .build();
        //设置旋转中心点
//        cxkEntity.setRotationOrigin(new Point2D(66, 71.5));
        double centerY = cxkEntity.getHeight() / 2;
        double centerX = cxkEntity.getWidth() / 2;
        cxkEntity.setRotationOrigin(new Point2D(centerX, centerY));
        Point2D center = cxkEntity.getCenter();
        System.out.println(center);
        double height = cxkEntity.getHeight();
        double width = cxkEntity.getWidth();
        System.out.println(height);
        System.out.println(width);
        FXGL.getGameWorld().addEntities(cxkEntity);
    }

    /**
     * 获取键盘事件
     */
    @Override
    protected void initInput() {
        //获取键盘向上箭头事件
        FXGL.getInput().addAction(
                new UserAction("Move up") {
                    @Override
                    protected void onAction() {
                        if (isMoving) {
                            return;
                        }
                        isMoving = true;
                        cxkEntity.translateY(-5);//y轴向上移动
                        cxkEntity.setRotation(0);//旋转角度
                    }
                }, KeyCode.UP
        );
        //向下移动
        FXGL.getInput().addAction(
                new UserAction("Move down") {
                    @Override
                    protected void onAction() {
                        if (isMoving) {
                            return;
                        }
                        isMoving = true;
                        cxkEntity.translateY(5);//y轴向下移动
                        cxkEntity.setRotation(180);//旋转角度
                    }
                }, KeyCode.DOWN
        );
        //向左移动
        FXGL.getInput().addAction(
                new UserAction("Move Left") {
                    @Override
                    protected void onAction() {
                        if (isMoving) {
                            return;
                        }
                        isMoving = true;
                        cxkEntity.translateX(-5);//x轴向左移动
                        cxkEntity.setRotation(270);//旋转角度
                    }
                }, KeyCode.LEFT
        );
        //向右边移动
        FXGL.getInput().addAction(
                new UserAction("Move Right") {
                    @Override
                    protected void onAction() {
                        if (isMoving) {
                            return;
                        }
                        isMoving = true;
                        cxkEntity.translateX(5);//x轴向右移动
                        cxkEntity.setRotation(90);//旋转角度

                    }
                }, KeyCode.RIGHT
        );
    }

    /**
     * 重写onUpdate,表示每一帧调用一次
     */
    @Override
    protected void onUpdate(double tpf) {
//        System.out.println(tpf);//每一帧用的时间
        isMoving = false;
    }


    /**
     * 启动方法
     */
    public static void main(String[] args) {
        launch(args);
    }
}
